package huixue.cn.com.rxapp.ui;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v7.widget.AppCompatSeekBar;
import android.widget.ImageView;
import android.widget.SeekBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import huixue.cn.com.library.util.PixelUtils;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.base.SimpleActivity;
import huixue.cn.com.rxapp.helper.ImageViewHelper;

public class BitmapColorActivity extends SimpleActivity implements AppCompatSeekBar.OnSeekBarChangeListener {

    @BindView(R.id.iv_timg)
    ImageView mIvTimg;

    Bitmap mBitmap;
    @BindView(R.id.sb_liangdu)
    AppCompatSeekBar sbLiangdu;
    @BindView(R.id.sb_baohedu)
    AppCompatSeekBar sbBaohedu;
    @BindView(R.id.sb_dubidu)
    AppCompatSeekBar sbDubidu;

    private int MAX_Value = 255;
    private int MID_Value = 127;

    private float liangdu,baohedu,duibidu;

    private Bitmap originBmp;

    @Override
    public int getLayout() {
        return R.layout.activity_bitmap_color;
    }

    @Override
    public void initView() {
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.timg);
        float scale = PixelUtils.getScreenWidth()*1.0f/mBitmap.getWidth();
        Matrix matrix = new Matrix();
        matrix.setScale(scale,scale);
        originBmp = Bitmap.createBitmap(mBitmap,0,0,mBitmap.getWidth(),mBitmap.getHeight(),matrix,true);

        mIvTimg.setImageBitmap(originBmp);

        sbLiangdu.setMax(MAX_Value);

        sbBaohedu.setMax(MAX_Value);

        sbDubidu.setMax(MAX_Value);

        sbLiangdu.setProgress(MID_Value);

        sbBaohedu.setProgress(MID_Value);

        sbDubidu.setProgress(MID_Value);

        //originBmp = BitmapFactory.decodeResource(getResources(),R.mipmap.timg);
    }

    @Override
    public void addEvent() {

        sbLiangdu.setOnSeekBarChangeListener(this);
        sbBaohedu.setOnSeekBarChangeListener(this);
        sbDubidu.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()){
            case R.id.sb_liangdu:
                liangdu = progress*1.0f/MID_Value;
                break;
            case R.id.sb_baohedu:
                baohedu = progress*1.0f/MID_Value;
                break;
            case R.id.sb_dubidu:
                duibidu = (progress-MID_Value)*1.0f/MID_Value*180;
                break;
        }

        Bitmap bmp = ImageViewHelper.handleImageViewHue(originBmp,liangdu,baohedu,duibidu);
        mIvTimg.setImageBitmap(bmp);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}
